﻿using System;
using System.Data;
using System.Windows.Forms;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.View.Profile;

namespace org.se.fs.view.Profile
{
    public partial class FrmBranchSubMenu : Form, IBranchSubMenu
    {
        #region Public Property Variable
        public void SetLabel(string status)
        {
            label.Text = status;
        }
        public void ButtonAddVisible(bool enable)
        {
            btnAdd.Visible = enable;
        }
        public void ButtonUpdateVisible(bool enable)
        {
            btnUpdate.Visible = enable;
        }
        public void ButtonAddEnable(bool enable)
        {
            btnAdd.Enabled = enable;
        }
        public void ButtonUpdateEnable(bool enable)
        {
            btnUpdate.Enabled = enable;
        }
        public string FullName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }
        public string Code
        {
            get { return txtCode.Text; }
            set { txtCode.Text = value; }
        }
        public string Address
        {
            get { return txtAddress.Text; }
            set { txtAddress.Text = value; }
        }
        public string Fax
        {
            get { return txtFax.Text; }
            set { txtFax.Text = value; }
        }
        public string Email
        {
            get { return txtEmail.Text; }
            set { txtEmail.Text = value; }
        }
        public string Mobile
        {
            get { return txtMobile.Text; }
            set { txtMobile.Text = value; }
        }
        public string Tel
        {
            get { return txtTel.Text; }
            set { txtTel.Text = value; }
        }
        public void FocusName()
        {
            txtName.Focus();
        }
        public void FocusCode()
        {
            txtCode.Focus();
        }
        public void FocusAddress()
        {
            txtAddress.Focus();
        }
        public void FocusFax()
        {
            txtFax.Focus();
        }
        public void FocusEmail()
        {
            txtEmail.Focus();
        }
        public void FocusMobile()
        {
            txtMobile.Focus();
        }
        public void FocusTel()
        {
            txtTel.Focus();
        }
        public bool IsModified { get; set; }
        #endregion

        private readonly BranchSubMenu _presenter;
        private DataTable _returnValue;

        public FrmBranchSubMenu()
        {
            InitializeComponent();
            _presenter = new BranchSubMenu(this);
            _presenter.OnAdd += PresenterOnAdd;
            _presenter.OnError += PresenterOnError;
            _presenter.OnModify += PresenterOnModify;
            _presenter.OnValidate += PresenterOnValidate;
        }

        static void PresenterOnValidate(object source, FormEvent e)
        {
            if (!e.IsSuccess)
                MessageBox.Show(e.Exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static DataTable FormShow(DataTable data)
        {
            using (var frmForm = new FrmBranchSubMenu())
            {
                frmForm._presenter.Data = data;
                frmForm.ShowDialog();
                return frmForm._returnValue;
            }
        }

        private void BtnSubmitClick(object sender, EventArgs e)
        {
            _presenter.Add();
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnUpdateClick(object sender, EventArgs e)
        {
            _presenter.Modify();
        }

        private void FrmFamilyBackgroundSubMenuLoad(object sender, EventArgs e)
        {
            _presenter.Initialize();
        }

        public void PresenterOnError(object source, FormEvent e)
        {
            if (!e.IsSuccess)
                MessageBox.Show(e.Exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void PresenterOnAdd(object source, FormEvent e)
        {
            MessageBox.Show(e.Exception.Message, e.IsSuccess ? @"Success" : @"Error", MessageBoxButtons.OK,
                            e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            
            if (!e.IsSuccess) return;
            _returnValue = (DataTable) e.Value;
            Close();
        }

        public void PresenterOnModify(object source, FormEvent e)
        {
            MessageBox.Show(e.Exception.Message, e.IsSuccess ? @"Success" : @"Error", MessageBoxButtons.OK,
                            e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);

            if (!e.IsSuccess) return;
            _returnValue = (DataTable)e.Value;
            Close();
        }
    }
}